﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Caching;
using DevToolBox.Common;
using DevToolBox.Cloud.Common;

namespace DevToolBox.Cloud.Local
{
    /// <summary>
    /// Class to abstract a local Cache. Only lasts while the program is running.
    /// </summary>
    public class ASPNetCache: ICacheable
    {
        private MemoryCache cacheClient = null;
        private CacheItemPolicy policy = null;
        private int minutesInCache;

        public ASPNetCache()
        {
            cacheClient = MemoryCache.Default;
            minutesInCache = ConfigParamReader.GetInt32("LocalCacheTimeMins");
            policy = new CacheItemPolicy();
            policy.Priority = CacheItemPriority.NotRemovable;
            policy.AbsoluteExpiration = DateTime.Now.AddMinutes(minutesInCache);

        }

        public void Set(string key, object value)
        {
            if (cacheClient != null && key != null)
            {
                cacheClient.Add(key, value, policy);
            }
        }

        public object Get(string key)
        {
            if (cacheClient == null || key == null) return null;
            return cacheClient.Get(key);
        }

    }
}
